<!doctype html>
<html>
<head>
<title>TUMensa</title>
<script>
(function()
{
	var 	options =
		{
			isSpeedDial: false,
			debug: true
		},
		paths = 
		{
			apiUrl: 'http://services.songbook.me/mensa/'
		};

	if (opera.contexts && opera.contexts.speeddial)
	{
		options.isSpeedDial = true;
		opera.contexts.speeddial.url = 'http://www.studentenwerk-muenchen.de/mensa/speiseplan/speiseplan_422_-de.html#heute';
	}

	/**
	 * loading the content
	 */
	window.addEventListener('load', function()
	{
		update();
	}, false);
	
	function update()
	{
		hide('data');
		show('loading');
		
		xhr = new XMLHttpRequest();
		var url = paths.apiUrl;
		d(url);
		xhr.open( 'GET', url, true );
		xhr.onreadystatechange=function()
		{
		    if( this.readyState!==4 )return false;

		    processData( this.responseXML );
		    //_updatePlaceTimer = setTimeout( _updatePlace, 1000*60*15 );
		}
		xhr.send(null);
	}
	
	function processData(xml)
	{
		hide('loading');
		show('data');
		
                var currentTime = new Date();
                var month = currentTime.getMonth() + 1;
                var day = currentTime.getDate();
                var year = currentTime.getFullYear();
                if (day < 10) day = "0"+day;
                if (month < 10) month = "0"+month;
                var date = day+"."+month+"."+year;
		
		var plans = xml.getElementsByTagName("Tagesplan");
		for (var i = 0; i < plans.length; i++)
		{
			var plan = plans[i];
			
			if (plan.getAttribute('tag') != date)
				continue;
			
			var dishes = plan.getElementsByTagName('Gericht');
			if (!dishes)
				return;
			
			var data = document.getElementById('data');
			for (var j = 0; j < dishes.length; j++)
			{
				var dish = dishes[j];
				d('found dish '+dish.getAttribute('name'));
				data.innerHTML += dish.getAttribute('name')+" ";
			}
		}
		/*plans.each(function(e)
		{
		}*/
	}
	
	function show(e)
	{
		if (typeof(e) == 'string')
			e = document.getElementById(e);
		e.style.display='block';
	}
	
	function hide(e)
	{
		if (typeof(e) == 'string')
			e = document.getElementById(e);
		e.style.display='none';
	}
	
	function d(m)
	{
		if (options.debug)
			opera.postError(m);
	}
})();
</script>
<link rel="stylesheet" href="style.css">
</head>
<body>
	<div id="loading"><img src="img/ajax-loader.gif"/></div>
	<div id="data"></div>
</body>
</html>
